<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello App</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <style type="text/css">
    body{
        margin:0;
        padding: 0;
    }
        /* CSS样式代码 */
        #page_index{            
            margin:0;
            padding: 0;
            position: relative;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body style="background: #fff;">
    <!-- HTML静态页面代码    -->
    <div id="page_index">
        <!-- 加载内容... -->
    </div>
</body>

<script type="text/javascript" src="../script/api.js"></script>

<script type="text/javascript">
    /*    JS逻辑代码    */
    let RequestData=new Object();
    function GetRequest(){//获取url中"?"符后的字符串
        let url = location.search;   
        let theRequest = new Object();
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            let strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }

        return theRequest
    }
    console.log('获取frame地址中的参数');

    RequestData=GetRequest();
    console.log(JSON.stringify(RequestData));

    let paramsData = {
        action: 'getPcChapterInfo',
        epubID:RequestData.epubID,
        consumeType: 1,
        platform: 3,
        deviceType: 'Android',
        deviceVersion: '5.0.0',
        channelId: 7000,
        platformSource: 'DDDS-P',
        fromPaltform: 'ds_android',
        deviceSerialNo: 'html5',
        clientVersionNo: '5.8.4',
        token: '',
        chapterID: RequestData.chapterID,
        pageIndex: RequestData.pageIndex,
        locationIndex: 0,
        wordSize: 2,
        style: 2,
        autoBuy: 0,
        chapterIndex: ''//破解之关键，应为空
    }

    function parseParam (json){
        let params=Object.keys(json).map(function (key) {
            return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
        }).join("&");
        return params
    }

    let urlData="http://e.dangdang.com/media/api.go?"+parseParam(paramsData);

    function getContents(urlData){//通过接口获取本页书的内容        
        $api.get(urlData,function(ret){
            if(ret.data.chapterInfo){
                let chapterInfo=JSON.parse(ret.data.chapterInfo);
                console.log(chapterInfo.status);
                if(chapterInfo.status='ok'){
                    document.getElementById('page_index').innerHTML=chapterInfo.snippet;
                    //console.log(chapterInfo.snippet);
                }
            }
        },'json'); 
    }

    apiready = function(){
        console.log(urlData);
        getContents(urlData);           
    }
</script>

</html>